define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            let vue = new Vue({
                el: '#vue',
                data: {
                    formLoading: false,
                    // 添加编辑
                    title: '',
                    addDialog: false,
                    // 表格当前页数据
                    tableData: [],
                    // 多选数组
                    multipleSelection: [],
                    // 搜索条件
                    key: '',
                    // 默认每页数据量
                    pageSize: 50,
                    // 当前页码
                    currentPage: 1,
                    // 默认数据总数
                    totalCount: 0,
                    // 列表加载遮罩
                    listLoading: false,
                    // 字段信息
                    formData: {
                        id: 0,
                        pid: '',
                        status: '1',
                        sort: '',
                        name: '',
                    },
                    // 表单验证规则
                    rules: {
                        name: [{
                            required: true,
                            message: '请您输入名称',
                            trigger: 'blur'
                        }, {
                            min: 2,
                            max: 50,
                            message: '名称应在2-50位字符之间'
                        }],
                    },
                    // 时间格式化
                    dateFormat: function (row, column) {
                        var date = row[column.property];
                        if (date === undefined || date === 0) {
                            return '';
                        }
                        return moment(date * 1000).format("YYYY-MM-DD HH:mm:ss");
                    }
                },
                created() {
                    this.loadData();
                },
                methods: {
                    // 状态改变
                    handleStatusChange(row) {
                        // 提交
                        let loading = this.$loading({
                            lock: true,
                            text: '正在更改中...',
                            spinner: 'el-icon-loading',
                            background: 'rgba(0, 0, 0, 0.3)'
                        });
                        $.ajax({
                            url: 'general/nav/status',
                            data: {
                                id: row.id,
                                status :row.status
                            },
                            type: 'GET',
                            success: res => {
                                loading.close();
                                if (res.code === 200) {
                                    this.$message.success(res.msg);
                                } else {
                                    this.$message.warning(res.msg);
                                }
                            },
                            error: function (er) {

                            }
                        });
                    },
                    // 提交
                    submit() {
                        this.$refs.form.validate(valid => {
                            if (!valid) {
                                return false;
                            }
                            // 提交
                            let loading = this.$loading({
                                lock: true,
                                text: '正在提交中...',
                                spinner: 'el-icon-loading',
                                background: 'rgba(0, 0, 0, 0.3)'
                            });
                            $.ajax({
                                url: 'general/nav/add',
                                type: 'POST',
                                data: this.formData,
                                success: res => {
                                    loading.close();
                                    if (res.code === 200) {
                                        this.$message.success(res.msg);
                                        this.addDialog = false;
                                        this.loadData();
                                    } else {
                                        this.$message.warning(res.msg);
                                    }
                                }
                            })
                        });
                    },
                    // 添加/编辑
                    openAddDialog(type, item) {
                        this.addDialog = true;
                        if (type === 'add') {
                            this.title = '添加';
                            this.formData = {
                                id: 0,
                                pid: '',
                                status: '1',
                                sort: '',
                                name: '',
                            };
                        } else {
                            this.title = '编辑';
                            this.formData = {
                                id: item.id,
                                name: item.name,
                                status: item.status,
                                sort: item.sort,
                                pid: item.pid === 0 ? '' : item.pid,
                            };
                        }
                    },
                    // 从服务器读取数据
                    loadData() {
                        this.listLoading = true;
                        $.getJSON('general/nav/index', {}, data => {
                            this.tableData = data.list;
                            this.listLoading = false;
                        });
                    },
                    // 排序
                    sortChange(e) {

                    },
                    // 每页显示数据量变更
                    handleSizeChange(val) {
                        this.pageSize = val;
                        this.loadData();
                    },
                    // 页码变更
                    handleCurrentChange(val) {
                        this.currentPage = val;
                        this.loadData();
                    },
                    // 多选响应
                    handleSelectionChange(val) {
                        this.multipleSelection = val;
                    },
                    // 单条删除
                    dell(id) {
                        this.$confirm('您确定要删除此分类吗？', '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning'
                        }).then(() => {
                            $.ajax({
                                url: 'general/nav/dell',
                                data: {
                                    id
                                },
                                type: "POST",
                                dataType: 'json',
                                success: res => {
                                    if (res.code === 200) {
                                        this.$message.success(res.msg);
                                        this.loadData();
                                    } else {
                                        this.$message.warning(res.msg);
                                    }
                                },
                                error: function (er) {

                                }
                            });
                        }).catch(() => {

                        });
                    },
                }
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
